package com.ilarele.shopping.client.activities;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

import com.ilarele.shopping.client.R;
import com.ilarele.shopping.client.util.Global;

public class MainTabActivity extends TabActivity {
	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab);
		Global.initGlobal(this);
		initViews();
		design();
	}

	private void design() {
		// tabs text color
		TabWidget tabWidget = tabHost.getTabWidget();
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			RelativeLayout rLayout = (RelativeLayout) tabWidget.getChildAt(i);
			TextView title = ((TextView) rLayout.getChildAt(1));
			title.setTextColor(Color.WHITE);
			title.setTextSize(15);
		}

		// tabs colors
		setTabColor(tabHost);
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				setTabColor(tabHost);
			}
		});
	}

	public static void setTabColor(TabHost tabHost) {
		TabWidget tabWidget = tabHost.getTabWidget();
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			// unselected
			tabWidget.getChildAt(i).setBackgroundResource(
					R.drawable.tab_unselected_gradient_color);
		}
		// selected
		tabWidget.getChildAt(tabHost.getCurrentTab()).setBackgroundResource(
				R.drawable.tab_selected_gradient_color);
	}

	private void initViews() {
		tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		intent = new Intent().setClass(this, A1OthersInfoActivity.class);
		spec = tabHost.newTabSpec("history")
				.setIndicator(getString(R.string.tab_others_info))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, A2MyInfoActivity.class);
		spec = tabHost.newTabSpec("ask_question")
				.setIndicator(getString(R.string.tab_my_info))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, A3SettingsActivity.class);
		spec = tabHost.newTabSpec("answer_question")
				.setIndicator(getString(R.string.tab_settings))
				.setContent(intent);
		tabHost.addTab(spec);

		// intent = new Intent().setClass(this, AutoGenerateDB.class);
		// spec = tabHost.newTabSpec("server_communication")
		// .setIndicator("Server Comm").setContent(intent);
		// tabHost.addTab(spec);

		tabHost.setCurrentTab(0);
	}
}
